﻿Public Class QuanLyChiForm
    Dim dt As DataTable
    Dim filter As String = ""
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        Me.Dispose()
    End Sub
    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Reload()
        rbTatCa.Checked = True
    End Sub
    Sub Reload()
        dt = QuanLyChiBUS.GetAll()
        dg.DataSource = dt
        dt.DefaultView.RowFilter = filter
    End Sub
    Private Sub QuanLyChiForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dg.Columns("MaPhieuChi").HeaderText = "Mã Phiếu Chi"
        dg.Columns("SoTien").HeaderText = "Số Tiền"
        dg.Columns("ChiTiet").HeaderText = "Chi Tiết"
        dg.Columns("TaiKhoan").HeaderText = "Tài Khoản"
        dg.Columns("Ngay").HeaderText = "Ngày chi"
        dg.Columns("loai_thu_nhap").HeaderText = "Loại chi phí"
        dg.Columns("ten_nguoi_lap").HeaderText = "Người Lập"
        dg.Columns("ten_nguoi_chi").HeaderText = "Người chi"
        dg.Columns("MaNganHang").Visible = False
        dg.Columns("MaUserChi").Visible = False
        dg.Columns("MaUserLap").Visible = False
        dg.Columns("MaChiPhi").Visible = False
        dg.Columns("thangthu").Visible = False
        dg.Columns("namthu").Visible = False
        cbMaUserLap.DataSource = UsersDAO.GetAll()
        cbMaUserLap.DisplayMember = "Fullname"
        cbMaUserLap.ValueMember = "MaUser"
        cbMaUserChi.DataSource = UsersDAO.GetAll()
        cbMaUserChi.DisplayMember = "Fullname"
        cbMaUserChi.ValueMember = "MaUser"
        cbMaNganHang.DataSource = NganHangDAO.GetAll()
        cbMaNganHang.DisplayMember = "Ten"
        cbMaNganHang.ValueMember = "MaNganHang"
    End Sub

    Private Sub btnLapPhieuChi_Click(sender As System.Object, e As System.EventArgs) Handles btnLapPhieuChi.Click
        Dim frm As New LapPhieuChiForm()
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Reload()
        End If
    End Sub

    Private Sub rbTatCa_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbTatCa.CheckedChanged
        filter = ""
        dt.DefaultView.RowFilter = filter
    End Sub

    Private Sub rbThangHienTai_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbThangHienTai.CheckedChanged
        filter = String.Format("thangthu = {0}", Date.Now.Month)
        dt.DefaultView.RowFilter = filter
    End Sub

    Private Sub rbNamHienTai_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbNamHienTai.CheckedChanged
        filter = String.Format("namthu = {0}", Date.Now.Year)
        dt.DefaultView.RowFilter = filter
    End Sub

    Private Sub dg_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dg.CellDoubleClick
        Dim frm As New LapPhieuChiForm(dg.CurrentRow.Cells("MaPhieuChi").Value)
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Reload()
        End If
    End Sub

    Private Sub btTim_Click(sender As System.Object, e As System.EventArgs) Handles btTim.Click
        Dim arr As New List(Of String)
        If (txtChiTiet.Text.Length > 0) Then
            arr.Add(String.Format("ChiTiet LIKE '%{0}%'", txtChiTiet.Text.Trim()))
        End If
        If (txtSoTien.Text.Length > 0) Then
            If (cbSoTien.SelectedIndex = 0) Then
                arr.Add(String.Format("SoTien >= {0} ", txtSoTien.Text))
            Else
                arr.Add(String.Format("SoTien <= {0} ", txtSoTien.Text))
            End If
        End If
        If cbMaUserLap.SelectedIndex >= 0 Then
            arr.Add(String.Format("MaUserLap = {0}", cbMaUserLap.SelectedValue))
        End If
        If cbMaUserChi.SelectedIndex >= 0 Then
            arr.Add(String.Format("MaUserChi = {0}", cbMaUserChi.SelectedValue))
        End If
        If cbMaNganHang.SelectedIndex >= 0 Then
            arr.Add(String.Format("MaNganHang = {0}", cbMaNganHang.SelectedValue))
        End If
        If dtNgay.Text.Length > 0 Then
            arr.Add(String.Format("Ngay <= #{0}#", dtNgay.Text))
        End If
        If (arr.Count > 0) Then
            Dim tim As String = String.Join(" and ", arr)
            dt.DefaultView.RowFilter = tim
        End If
    End Sub
End Class